package top.cluski.feieprint.api.teable.domain.dto;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSONArray;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import top.cluski.feieprint.api.teable.TeableApi;
import top.cluski.feieprint.api.teable.entity.RecordEntity;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import static top.cluski.feieprint.api.teable.entity.Filter.emptyFilter;

/**
 * 起步时长  30分钟（10公里时长），超出10公里，每公里加2分钟
 * 每天的15:30-18:30时间段下班高峰，会堵车，时间规则改下，每公里3分钟
 */
@Data
@Accessors(chain = true)
@Slf4j
public class ShansongConfig {
    int firstPeriodMinutes = 30;
    int minutesPerMile = 2;
    int highMinusPerMile = 3;
    List<LocalTime[]> highTimePeriods = new ArrayList<>();

    public static ShansongConfig create() {
        ShansongConfig shansongConfig = new ShansongConfig();
        shansongConfig.highTimePeriods.add(new LocalTime[]{LocalTime.of(15, 30, 0),
                LocalTime.of(18, 30, 0)});
        return shansongConfig;
    }

    public static ShansongConfig fromTeable() {
        ShansongConfig shansongConfig = create();
        try {
            TeableApi teableApi = SpringUtil.getBean(TeableApi.class);
            List<RecordEntity> records = teableApi.getRecords("tblR87JMmLOhwzeow79", emptyFilter());
            for (RecordEntity record : records) {
                String title = record.getFields().getString("标题");
                if (StrUtil.equals("汽车首10公里时间（分钟）", title)) {
                    shansongConfig.firstPeriodMinutes = Integer.parseInt(record.getFields().getString("值"));
                } else if (StrUtil.equals("汽车后续公里每公里时间（分钟）", title)) {
                    shansongConfig.minutesPerMile = Integer.parseInt(record.getFields().getString("值"));
                } else if (StrUtil.equals("汽车高峰时段", title)) {
                    shansongConfig.highTimePeriods.clear();
                    String value = record.getFields().getString("值");
                    JSONArray jsonArray = JSONArray.parse(value);
                    for (int i = 0; i < jsonArray.size(); i++) {
                        String string = jsonArray.getString(i);
                        String[] split = string.split("-");
                        LocalTime start = LocalTime.parse(split[0], DateTimeFormatter.ofPattern("HH:mm"));
                        LocalTime end = LocalTime.parse(split[1], DateTimeFormatter.ofPattern("HH:mm"));
                        shansongConfig.highTimePeriods.add(new LocalTime[]{start, end});

                    }
                } else if (StrUtil.equals("汽车高峰时段后续公里时间（分钟）", title)) {
                    shansongConfig.highMinusPerMile = Integer.parseInt(record.getFields().getString("值"));
                }
            }
        } catch (Exception e) {
            log.error("从teable中获取配送时间配置失败", e);
            shansongConfig = create();
        }
        return shansongConfig;
    }
}
